#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t mtx;
pthread_cond_t cond;
int money=0;

void* handle(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mtx);
        if(money<100)
        {
            pthread_cond_wait(&cond,&mtx);
        }
        while(money>=100)
        {
            printf("%d\n",money);
            money-=100;
        }
        printf("%d\n",money);
        pthread_mutex_unlock(&mtx);
    }
    return NULL;
}

int main(int argc , char const *argv[])
{
    pthread_t tid;

    if (pthread_cond_init(&cond,NULL)!=0)
    {
        perror("cond init err\n");
        return -1;
    }
    if (pthread_mutex_init(&mtx,NULL)==-1)
    {
        perror("mtx init err\n");
        pthread_cond_destroy(&cond);
        return -1;
    }
    if(pthread_create(&tid,NULL,handle,NULL)!=0)
    {
        perror("pthread create err\n");
        pthread_mutex_destroy(&mtx);
        pthread_cond_destroy(&cond);
        return -1;
    }

    while (1)
    {
        usleep(100);
        pthread_mutex_lock(&mtx);
        printf("please input the money:\n");
        scanf("%d",&money);
        if(money>=100)        
        {
            pthread_cond_signal(&cond);        
        }
        pthread_mutex_unlock(&mtx);
        sleep(3);
    }

    pthread_join(tid,NULL);
    pthread_mutex_destroy(&mtx);
    pthread_cond_destroy(&cond);

    return 0;
}